<x-shuguo-layout>
    <x-slot name="header">
        <form class="layui-form" id="form" action="{{  route('goods_doAdd')  }}" method="post">
        <div class="layui-form-item field-article-content">
            <label class="layui-form-label" for="article-content">果蔬名称</label>
            <div class="layui-input-block">
                <input type="text" name="title" class="layui-input" placeholder="比如:香蕉" />
            </div>
            <div class="help-block"></div>
        </div>
        <div class="layui-form-item field-article-content">
            <label class="layui-form-label" for="article-content">单价</label>
            <div class="layui-input-block">
                <input type="text" name="price" class="layui-input" placeholder="单位:元" />
            </div>
            <div class="help-block"></div>
        </div>
        <div class="layui-form-item field-article-content">
            <label class="layui-form-label" for="article-content">库存</label>
            <div class="layui-input-block">
                <input type="text" name="number" class="layui-input" />
            </div>
            <div class="help-block"></div>
        </div>
        <div class="layui-form-item field-article-content">
            <label class="layui-form-label" for="article-content">果蔬种类</label>
            <div class="layui-input-block">
                <select name="goods_type" class="goodsType" lay-verify="" id="goodsType">
                    @foreach ($goodsTypeData as $key => $val)
                        <option value="{{ $key }}">{{ $val }}</option>
                    @endforeach
                </select>
            </div>
            <div class="help-block"></div>
        </div>

        <div class="layui-form-item field-article-author">
            <label class="layui-form-label" for="article-author">上传图片</label>
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="file_pic">
                    <i class="layui-icon">&#xe67c;</i>上传图片
                </button>
                <img id="imageId" style="width:160px;" src="">
            </div>
            <div class="help-block"></div>
        </div>
        <input id="pic"  name="pic" type="hidden"  value="">

        <hr class="layui-bg-gray">

        <div class="layui-form-item">
            <label class="layui-form-label">

            </label>
            <div class="layui-input-block">
                <a class="layui-btn layui-btn-sm" lay-filter="submit" lay-submit
                   action="{{  route('goods_doAdd')  }}" adminId=""><i class="layui-icon"></i>添加</a>
            </div>

        </div>
        </form>

        <script>
            layui.use(['form','layer','laydate','upload'],function(){
                var form = layui.form,
                    layer = parent.layer === undefined ? layui.layer : top.layer,
                    laypage = layui.laypage,
                    layedit = layui.layedit,
                    laydate = layui.laydate,
                    $ = layui.jquery;

                form.on("submit(submit)",function(data){
                    var action = $(this).attr('action');
                    var data = $('#form').serialize();
                    //弹出loading
                    var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
                    //实际使用时的提交信息
                    $.post(action,data,function(res){
                        var _obj = eval('(' + res + ')')
                        if(_obj.code == AdminConstant.code_success){
                            top.layer.msg("操作成功！");
                            setTimeout(function(){
                                top.layer.close(index);
                                layer.closeAll("iframe");
                                //刷新父页面
                                parent.location.reload();
                            },500);
                        }else{
                            top.layer.msg("操作失败！");
                        }
                    })
                    return false;
                })




                var upload = layui.upload;

                //执行实例
                var uploadInst = upload.render({
                    elem: '#file_pic' //绑定元素
                    ,url: '/upload/pic' //上传接口
                    ,method: 'post'  //可选项。HTTP类型，默认post
                    ,accept: 'file' //允许上传的文件类型
                    ,size: 100000 //最大允许上传的文件大小
                    ,done: function(res){
                        $("#imageId").attr("src",res.data.src);
                        $('#pic').val(res.data.src);
                        top.layer.msg("上传成功！");
                        //上传完毕回调
                    }
                    ,error: function(){
                        //请求异常回调
                        top.layer.msg("上传失败！");
                    }
                });



            })
        </script>
        <script>
            layui.use('layedit', function(){
                var layedit = layui.layedit
                    ,$ = layui.jquery;
                layedit.set({
                    uploadImage: {
                        url: '/upload/file' //接口url
                        ,type: 'post' //默认post
                    }
                });
                var index = layedit.build('textarea'); //建立编辑器
                //编辑器外部操作
                var active = {
                    content: function(){
//                console.log(layedit.getContent(index)); //获取编辑器内容
                        $('#content').val(layedit.getContent(index));
                        top.layer.msg("换取了");
                    }
//            ,text: function(){
//                alert(layedit.getText(index)); //获取编辑器纯文本内容
//            }
//            ,selection: function(){
//                alert(layedit.getSelection(index));
//            }
                };

                $('.site-demo-layedit').on('click', function(){
                    var type = $(this).data('type');
                    active[type] ? active[type].call(this) : '';
                });




                //自定义工具栏
//        layedit.build('textarea', {
//            tool: ['face', 'link', 'unlink', '|', 'left', 'center', 'right']
//            ,height: 400
//        })
            });


        </script>


    </x-slot>

</x-shuguo-layout>